package com.jyh.blog.controller;

import com.jyh.blog.annotation.OptLog;
import com.jyh.blog.dto.*;
import com.jyh.blog.service.ArticleService;
import com.jyh.blog.strategy.context.UploadStrategyContext;
import com.jyh.blog.utils.Result;
import com.jyh.blog.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.util.List;

import static com.jyh.blog.constant.OptTypeConst.*;
import static com.jyh.blog.enums.FilePathEnum.ARTICLE;

@Api(tags = "文章管理")
@RestController
public class ArticleController {
    @Autowired
    private ArticleService articleService;

    @Autowired
    private UploadStrategyContext uploadStrategyContext;

    /**
     * 查看文章归档
     * @return
     */
    @ApiOperation(value = "查看文章归档")
    @GetMapping("articles/archives")
    public PageResult<ArchiveDTO> archives(){
        return articleService.archives();
    }

    /**
     * 首页文章列表
     *
     * @return
     */
    @ApiOperation(value = "首页文章列表")
    @GetMapping("articles")
    public Result<List<ArticleHomeDTO>> list() {
        return Result.ok(articleService.articleHomeDTOList());
    }

    /**
     * 根据id查询文章
     * @param id
     * @return
     */
    @ApiOperation(value = "根据id查询文章")
    @GetMapping("articles/{id}")
    public Result<ArticleDTO> articleDetails(@PathVariable("id") String id) {
       return Result.ok(articleService.articleDetails(id));
    }

    /**
     * 条件查询文章
     * @param conditionVO
     * @return
     */
    @ApiOperation(value = "条件查询文章")
    @GetMapping("articles/condition")
    public Result<ArticlePreviewListDTO> articleByCondition(ConditionVO conditionVO){
        return Result.ok(articleService.articleByCondition(conditionVO));
    }

    /**
     * 搜索文章
     * @param conditionVO
     * @return
     */
    @ApiOperation(value = "搜索文章")
    @GetMapping("articles/search")
    public Result<List<ArticleSearchDTO>> articleSearchList(ConditionVO conditionVO){
        return Result.ok(articleService.articleSearchList(conditionVO));
    }


    /**
     * 后台文章列表
     *
     * @param conditionVo
     * @return
     */
    @ApiOperation(value = "后台文章列表")
    @PostMapping("admin/articles")
    public Result<PageResult<ArticleBackDTO>> backList(@RequestBody ConditionVO conditionVo) {
        PageResult<ArticleBackDTO> articleBackList = articleService.articleBackList(conditionVo);
        return Result.ok(articleBackList);
    }

    /**
     * 添加或修改文章
     *
     * @param articleVO
     * @return
     */
    @OptLog(optType = SAVE_OR_UPDATE)
    @ApiOperation(value = "添加或修改文章")
    @PostMapping("admin/article")
    public Result saveOrUpdateArticle(@RequestBody ArticleVO articleVO) {
        articleService.saveOrUpdateArticle(articleVO);
        return Result.ok();
    }

    /**
     * 保存文章图片
     *
     * @param file
     * @return
     */
    @ApiOperation(value = "保存文章图片")
    @PostMapping("admin/article/images")
    public Result<String> uploadImg(MultipartFile file) {
        return Result.ok(uploadStrategyContext.executeUploadStrategy(file, ARTICLE.getPath()));
    }

    /**
     * 根据id获取后台文章
     *
     * @param articleId
     * @return
     */
    @ApiOperation(value = "根据id查询后台文章")
    @GetMapping("admin/article/{id}")
    public Result<ArticleVO> getBackArticleById(@PathVariable("id") String articleId) {
        return Result.ok(articleService.getBackArticleById(articleId));
    }

    /**
     * 逻辑删除与恢复文章
     *
     * @param deleteVO
     * @return
     */
    @OptLog(optType = UPDATE)
    @ApiOperation(value = "逻辑删除与恢复文章")
    @PutMapping("admin/article")
    public Result logicDelete(@Valid @RequestBody DeleteVO deleteVO) {
        articleService.logicDelete(deleteVO);
        return Result.ok();
    }

    /**
     * 物理删除文章
     *
     * @param idList
     * @return
     */
    @OptLog(optType = REMOVE)
    @ApiOperation(value = "物理删除文章")
    @DeleteMapping("admin/article")
    public Result deleteArticle(@RequestBody List<String> idList) {
        articleService.deleteArticle(idList);
        return Result.ok();
    }

    /**
     * 置顶文章
     *
     * @param articleTopVO
     * @return
     */
    @OptLog(optType = UPDATE)
    @ApiOperation(value = "置顶文章")
    @PutMapping("admin/article/top")
    public Result changeTop(@Valid @RequestBody ArticleTopVO articleTopVO) {
        articleService.changTop(articleTopVO);
        return Result.ok();
    }

    /**
     * 导入文章
     *
     * @param file
     * @param type
     * @return
     */
    @ApiOperation(value = "导入文章")
    @PostMapping("admin/article/import")
    public Result importArticle(MultipartFile file, @RequestParam(required = false) String type) {
        articleService.importArticle(file,type);
        return Result.ok();
    }

    /**
     * 导出文章
     *
     * @param idList
     * @return
     */
    @ApiOperation(value = "导出文章")
    @PostMapping("admin/article/export")
    public Result<List<String>> exportArticle(@RequestBody List<String> idList) {
        return Result.ok(articleService.exportArticle(idList));
    }

    /**
     * 文章点赞
     * @param articleId
     * @return
     */
    @ApiOperation(value = "文章点赞")
    @PostMapping("article/{articleId}/like")
    public Result articleLike(@PathVariable("articleId") String articleId){
        articleService.articleLike(articleId);
        return Result.ok();
    }
}
